//
//  OTEditGroupViewController.m
//  OT_OC
//
//  Created by yunmian on 2019/7/16.
//  Copyright © 2019 James. All rights reserved.
//

#import "OTEditGroupViewController.h"
#import "OTGroupModel.h"
#import "OTEditGroupCell.h"
#import "OTAddGroupVC.h"

@interface OTEditGroupViewController ()<UITableViewDelegate, UITableViewDataSource, OTNavBarDelegate, OTAddGroupVCDelegate>
{
    UITableView             *tableView;
    NSMutableArray          *datasource;
}
@property (nonatomic, strong) OTAddGroupVC *popoverVC;
@end

@implementation OTEditGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    [self initData];
}

- (void)initUI {
    
    self.view.backgroundColor = UIColor.whiteColor;
    
    OTNavBar *navBar = [OTNavBar createNavBar];
    navBar.type = OTNavBarTypeAdd;
    navBar.delegate = self;
    navBar.rightTitle = @"添加项目组";
    [self.view addSubview:navBar];
    
    tableView = [[UITableView alloc] init];
    tableView.backgroundColor = UIColor.whiteColor;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.tableFooterView = [UIView new];
    [tableView registerClass:[OTEditGroupCell class] forCellReuseIdentifier:@"OTEditGroupCell"];
    [self.view addSubview:tableView];
}

- (void)initData {

    datasource = [NSMutableArray array];
    
//    NSArray *groupArray = [OTGroupModel bg_findAll:group_tablename];
//    if (![OTTool isArrayEmptyWithArray:groupArray]) {
//        [datasource setArray:groupArray];
//    }else {
        NSArray *tempArray = @[@"润工作",@"润工作1",@"润工作2"];
        for (NSInteger i = 0; i < tempArray.count; i++) {
            OTGroupModel *group = [OTGroupModel new];
            group.groupName = tempArray[i];
//            group.bg_tableName = group_tablename;
            group.list = @[];
//            [group bg_save];
        }
//        NSArray *groupArray1 = [OTGroupModel bg_findAll:group_tablename];
//        [datasource setArray:groupArray1];
//    }
    [tableView reloadData];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(k_Height_NavBar, 0, 0, 0));
    }];
}

#pragma mark UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OTEditGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OTEditGroupCell" forIndexPath:indexPath];
    
    OTGroupModel *group = datasource[indexPath.row];
    cell.group = group;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    kWeakSelf(self);
    //删除
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [weakself deleteGroupWithIndex:indexPath.row];
    }];
    //编辑
    UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [weakself editGroupWithIndexPath:indexPath];
    }];
    
    deleteRowAction.backgroundColor = ColorHexFF7741;
    editRowAction.backgroundColor = ColorHex85E3F7;
    return @[deleteRowAction,editRowAction];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OTGroupModel *group = datasource[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(editGroupDidSelectedGroup:)]) {
        [self.delegate editGroupDidSelectedGroup:group.groupName];
    }
    [self zw_toLastViewController];
}

#pragma mark OTNavBarDelegate
- (void)navBarDidSelectedRight {
    [self zw_showAlertViewController:self.popoverVC];
}

#pragma mark OTAddGroupVCDelegate
- (void)confirmGroupWithText:(NSString *)text {
    if ([OTTool isStringEmptyWithText:text]) {
        return;
    }
    
    OTGroupModel *group = [OTGroupModel new];
    group.groupName = text;
//    group.bg_tableName = group_tablename;
    group.list = @[];
//    [group bg_save];
    
    [datasource addObject:group];
    
    [tableView reloadData];
}

- (void)confirmEditGroupWithText:(NSString *)text indexPath:(NSIndexPath *)indexPath {
    OTGroupModel *group = datasource[indexPath.row];
    
    OTGroupModel *g = [OTGroupModel new];
//    g.bg_tableName = group_tablename;
    g.groupName = text;
    
    if ([OTTool isStringEmptyWithText:text]) {
        return;
    }
    
    
    
//    NSString *where = [NSString stringWithFormat:@"where %@='%@'",bg_sqlKey(@"groupName"),group.groupName];
    
//    if ([g bg_updateWhere:where]) {
        [datasource replaceObjectAtIndex:indexPath.row withObject:g];
        [tableView reloadData];
//    }else {
//        [OTTool showToastWithText:@"编辑失败~"];
//    }
}

#pragma mark Private
- (void)deleteGroupWithIndex:(NSInteger)index {
    OTGroupModel *group = datasource[index];
    
//    NSString *where = [NSString stringWithFormat:@"where %@='%@'",bg_sqlKey(@"groupName"),group.groupName];
//    if ([OTGroupModel bg_delete:group_tablename where:where]) {
//        [OTGroupModel bg_delete:group_tablename where:where];
        [datasource removeObjectAtIndex:index];
        [tableView reloadData];
//    }else {
//        [OTTool showToastWithText:@"删除失败~"];
//    }
}

- (void)editGroupWithIndexPath:(NSIndexPath *)indexPath {
    OTGroupModel *group = datasource[indexPath.row];
    self.popoverVC.type = OTAddGroupVCTypeGroup;
    self.popoverVC.indexPath = indexPath;
    self.popoverVC.groupName = group.groupName;
    [self zw_showAlertViewController:self.popoverVC];
}

#pragma mark Lazy
- (OTAddGroupVC *)popoverVC {
    if (!_popoverVC) {
        _popoverVC = [OTAddGroupVC new];
        _popoverVC.delegate = self;
    }
    return _popoverVC;
}
@end
